import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:macnov/common/empty_data.dart';
import 'package:macnov/common/loading.dart';
import 'package:macnov/models/transaction.dart';
import 'package:macnov/utils/Api.dart';

class ReceiptImage extends StatefulWidget {
  final TransactionModel txnModel;

  const ReceiptImage({Key key, this.txnModel}) : super(key: key);

  @override
  _ReceiptImageState createState() => _ReceiptImageState(txnModel);
}

class _ReceiptImageState extends State<ReceiptImage> {
  final TransactionModel txnModel;
  _ReceiptImageState(this.txnModel);
  List<int> bytedata;

  @override
  void initState() {
    super.initState();
    _loadReceiptData();
  }

  _loadReceiptData() async {
    if (txnModel.receipt == null) return;
    List<int> datas = await Api.getReceipt(txnModel.receipt);
    setState(() {
      bytedata = new Uint8List.fromList(datas);
    });
  }

  @override
  Widget build(BuildContext context) {
    if (txnModel.receipt == null)
      return Container(
        padding: EdgeInsets.fromLTRB(20, 20, 20, 40),
        child: EmptyData(),
      );
    if (bytedata == null) return Loading();
    return Container(
      padding: EdgeInsets.fromLTRB(20, 20, 20, 40),
      child: Image.memory(
        bytedata,
      ),
    );
  }
}
